package org.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TopicExchange 模式
 *
 *@ClassName: TopicRabbitConfig
 *@Author: CJ
 *@Date: 2021-9-24 0:40
 */
@Configuration
public class TopicRabbitConfig {

    /*绑定键*/
    public final static String MAN = "topic.man";
    public final static String WOMAN = "topic.woman";

    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.MAN, true);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.WOMAN, true);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange", true, false);
    }

    /*将firstQueue和topicExchange绑定,而且绑定的键值为topic.man*/
    /*这样只要是消息携带的路由键是topic.man，才会分发到该队列*/
    @Bean
    public Binding bindingExchangeMessage1() {
        return BindingBuilder.bind(firstQueue()).to(topicExchange()).with("topic.man");
    }

    /*将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#*/
    /*这样只要是消息携带的路由键是以topic.开头，都会分发到该队列*/
    @Bean
    public Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(topicExchange()).with("topic.#");
    }
}